//
//  CustomSegmentedControl.m
//  YoucApp
//
//  Created by 21CN Vgo on 12/10/12.
//
//

#import "CustomSegmentedControl.h"
#import "UIUtil.h"

@interface CustomSegmentedControl ()

@end

@implementation CustomSegmentedControl

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
		[self addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventValueChanged];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
	self = [super initWithCoder:aDecoder];
	
	if (self)
	{
		// Initialization code
		[self addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventValueChanged];
	}
	
	return self;
}

- (void)valueChanged
{
	[self setNeedsDisplay];
}

UIFont * getSegmentedFont(UIView * aView)
{
	UIFont * font = nil;
	
	NSString * typeName = NSStringFromClass([aView class]);
	if ([typeName compare:@"UISegmentLabel" options:NSLiteralSearch] == NSOrderedSame) {
		UILabel* label = (UILabel*)aView;
		font = [[label.font retain] autorelease];
	}
	
	if (!font)
	{
		NSArray* subs = [aView subviews];
		NSEnumerator* iter = [subs objectEnumerator];
		UIView* subView;
		while ((subView = [iter nextObject]) && !font) {
			font = getSegmentedFont(subView);
		}
	}
	
	return font;
}

- (void)drawWithImages:(NSArray *)images andColors:(NSArray *)colors
{
	NSArray * normalImages = [images objectAtIndex:0];
	NSArray * selectedImages = [images objectAtIndex:1];
	
	CGFloat width = 0.0;
	CGFloat height = 0.0;
	
	NSUInteger selectedSegmentIndex = self.selectedSegmentIndex;
	
	NSMutableArray * array = [NSMutableArray array];
	
	for (NSUInteger i=0; i < self.numberOfSegments; i++)
	{
		if (i == 0)
		{
			[array addObject:selectedSegmentIndex == i ? [selectedImages objectAtIndex:0] : [normalImages objectAtIndex:0]];
		}
		else if (i == self.numberOfSegments - 1)
		{
			[array addObject:selectedSegmentIndex == i ? [selectedImages lastObject] : [normalImages lastObject]];
		}
		else
		{
			NSUInteger index = [selectedImages count] > 1 ? 1 : 0;
			
			[array addObject:selectedSegmentIndex == i ? [selectedImages objectAtIndex:index] : [normalImages objectAtIndex:index]];
		}
		UIImage * image = (UIImage*)[array lastObject];
		width += image.size.width;
		if (height < image.size.height)
			height = image.size.height;
	}

	CGContextRef context = UIGraphicsGetCurrentContext();
	
	width = 0.0;
	
	for (NSUInteger i=0; i < [array count]; i++)
	{
		UIImage * image = (UIImage*)[array objectAtIndex:i];
		
		CGRect imageRect = CGRectMake(width, 0, self.bounds.size.width / [array count], self.bounds.size.height);
		//[image drawAtPoint:imageRect.origin];
		[image drawInRect:imageRect];
		
		NSString * text = [self titleForSegmentAtIndex:i];

		UIColor *textColor = selectedSegmentIndex == i ? [colors objectAtIndex:1] : [colors objectAtIndex:0];
		CGContextSetFillColorWithColor(context, textColor.CGColor);
		
		UIFont * textFont = getSegmentedFont(self);
		CGSize textSize = [text sizeWithFont:textFont];
		imageRect.origin.y = (imageRect.size.height - textSize.height) / 2 + 1;
		[text drawInRect:imageRect withFont:textFont lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentCenter];
		
		width += imageRect.size.width;
	}
}

- (void)layoutSubviews
{
	[super layoutSubviews];
	
	for (UIView *view in self.subviews)
	{
		view.alpha = 0.02;
	}
	
	[self setNeedsDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
	NSArray * images = [NSArray arrayWithObjects:[self normalImages], [self selectedImages], nil];
	NSArray * colors = [NSArray arrayWithObjects:[self normalColor], [self selectedColor], nil];
	[self drawWithImages:images andColors:colors];
}

- (NSArray *)normalImages
{
	UIImage * imageUnFocus = [UIImage imageNamed:@"wy_list_segment_unfocus.png"];
	NSArray * normalImages = [NSArray arrayWithObjects:imageUnFocus, nil];
	return normalImages;
}

- (NSArray *)selectedImages
{
	UIImage * imageFocus = [UIImage imageNamed:@"wy_list_segment_focus.png"];
	NSArray * selectedImages = [NSArray arrayWithObjects:imageFocus, nil];
	return selectedImages;
}

- (UIColor *)normalColor
{
	return [UIColor lightGrayColor];
}

- (UIColor *)selectedColor
{
	return [UIColor blackColor];
}

@end
